/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmOrderDetails.java
 *
 * Created on May 24, 2011, 4:57:15 AM
 */

package Admin;

import Account.SizeMonition;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author BachDEV
 */
public class frmOrderDetails extends javax.swing.JFrame {

    JTable tblOrderDetails;
    frmAdminCenter admin;
    public static int x;
    public static int y;

    /** Creates new form frmOrderDetails */
    public frmOrderDetails(frmAdminCenter admin) {
        try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(InstantiationException ex){
            ex.printStackTrace();
        }catch(UnsupportedLookAndFeelException ex){
            ex.printStackTrace();
        }catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }catch(IllegalAccessException ex){
            ex.printStackTrace();
        }
        this.admin = admin;
        try {
            Thread.sleep(1000);
            initComponents();
            Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
            x = this.getWidth();
            y = this.getHeight();
            this.setLocation((ds.width - this.getWidth()) / 2, (ds.height - this.getHeight()) / 2);
            tblOrderDetails = buildTableOrderDetails();
            scrOrderDetails.setViewportView(tblOrderDetails);
            setField();
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }

    }

    public void motion() {
        this.setSize(0, 0);
        new Thread(new SizeMonition(this, x, y)).start();
    }
///////////////////////////////////////////////////////////////////////////////
      public void setField() throws Exception {
        Connection connect = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String sql = "Select cusName,cusAddress,cusPhone,cusEmail,ordName,ordAddress,ordPhone,ordID,ordDate from Orders join Customer on Orders.cusID=Customer.cusID where ordID=?";
        try {
            connect = Connections.DBConnection.getConnection("eProject", "sa", "123");
            stmt = connect.prepareStatement(sql);
            stmt.setString(1, admin.tblOrder.getValueAt(admin.tblOrder.getSelectedRow(), 0).toString().trim());
            rs = stmt.executeQuery();
            if (rs.next()) {
                lbCustomerName.setText(rs.getString(1) + "");
                lbCustomerAddress.setText(rs.getString(2) + "");
                lbCustomerPhone.setText(rs.getString(3) + "");
                lbCustomerEmail.setText(rs.getString(4) + "");
                lbRecipientName.setText(rs.getString(5) + "");
                lbRecipientAddress.setText(rs.getString(6) + "");
                lbRecipientPhone.setText(rs.getString(7) + "");
                lbOrderID.setText(rs.getInt(8) + "");
                lbOrderDate.setText(rs.getDate(9) + "");
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            try {
                Connections.DBConnection.closeConnection(connect, stmt, rs);
            } catch (Exception ex) {
                throw ex;
            }
        }
    }

    public JTable buildTableOrderDetails() throws Exception {
        JTable table = null;
        Vector column = new Vector();
        column.add("Product Name");
        column.add("Quantity");
        column.add("Price");
        column.add("Amount");
        Vector data = new Vector();
        Vector row;
        Connection connect = null;
        Connection connectTotal = null;
        PreparedStatement stmt = null;
        PreparedStatement stmtTotal = null;
        ResultSet rs = null;
        ResultSet rsTotal = null;
        String sql = "select proName,Quantity,Price,Quantity*Price as Amount from OrderDetails join Product on OrderDetails.proID = Product.proID where ordID = ?";
        String sqlTotal = "select sum(Quantity*Price) as Total from OrderDetails where ordID = ?";
        try {
            connect = Connections.DBConnection.getConnection("eProject", "sa", "123");
            stmt = connect.prepareStatement(sql);
            stmt.setString(1, admin.tblOrder.getValueAt(admin.tblOrder.getSelectedRow(), 0).toString().trim());
            rs = stmt.executeQuery();
            while (rs.next()) {
                row = new Vector();
                row.add(rs.getString(1));
                row.add(rs.getInt(2));
                row.add(rs.getDouble(3) + "$");
                row.add(rs.getDouble(4) + "$");
                data.add(row);
            }
            connectTotal = Connections.DBConnection.getConnection("eProject", "sa", "123");
            stmtTotal = connectTotal.prepareStatement(sqlTotal);
            stmtTotal.setString(1, admin.tblOrder.getValueAt(admin.tblOrder.getSelectedRow(), 0).toString().trim());
            rsTotal = stmtTotal.executeQuery();
            if (rsTotal.next()) {
                row = new Vector();
                row.add("Total value of orders");
                row.add("");
                row.add("");
                row.add(rsTotal.getDouble(1) + "$");
                data.add(row);
            }
            table = new JTable(data, column);
        } catch (Exception ex) {
            throw ex;
        }
        return table;

    }

//////////////////////////////////////////////////////////////////////////////
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbOrderDate = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lbOrderID = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel14 = new javax.swing.JLabel();
        lbCustomerPhone = new javax.swing.JLabel();
        lbCustomerName = new javax.swing.JLabel();
        lbCustomerAddress = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        lbCustomerEmail = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel17 = new javax.swing.JLabel();
        lbRecipientPhone = new javax.swing.JLabel();
        lbRecipientName = new javax.swing.JLabel();
        lbRecipientAddress = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        scrOrderDetails = new javax.swing.JScrollPane();
        btnExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Order Details");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        lbOrderDate.setText("Order Date: ");

        jLabel3.setText("Order Date: ");

        lbOrderID.setText("Order ID: ");

        jLabel2.setText("Order ID: ");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Order Details");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Customer information"));

        jLabel14.setText("Phone");

        lbCustomerPhone.setForeground(new java.awt.Color(0, 0, 153));
        lbCustomerPhone.setText("Customer  Phone");

        lbCustomerName.setForeground(new java.awt.Color(0, 0, 153));
        lbCustomerName.setText("Customer name");

        lbCustomerAddress.setForeground(new java.awt.Color(0, 0, 153));
        lbCustomerAddress.setText("Customer  Address");

        jLabel16.setText("Email");

        jLabel12.setText("Address");

        jLabel15.setText("Name");

        lbCustomerEmail.setForeground(new java.awt.Color(0, 0, 153));
        lbCustomerEmail.setText("Customer  Email");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(jLabel16)
                    .addComponent(jLabel12)
                    .addComponent(jLabel15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbCustomerName)
                    .addComponent(lbCustomerAddress)
                    .addComponent(lbCustomerEmail)
                    .addComponent(lbCustomerPhone))
                .addGap(111, 111, 111))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(lbCustomerName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(lbCustomerAddress))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(lbCustomerPhone))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(lbCustomerEmail))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Recipient information"));

        jLabel17.setText("Phone");

        lbRecipientPhone.setForeground(new java.awt.Color(0, 0, 153));
        lbRecipientPhone.setText("Recipient  Phone");

        lbRecipientName.setForeground(new java.awt.Color(0, 0, 153));
        lbRecipientName.setText("Recipient name");

        lbRecipientAddress.setForeground(new java.awt.Color(0, 0, 153));
        lbRecipientAddress.setText("Recipient  Address");

        jLabel19.setText("Address");

        jLabel20.setText("Name");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel17)
                    .addComponent(jLabel19)
                    .addComponent(jLabel20))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbRecipientName)
                    .addComponent(lbRecipientAddress)
                    .addComponent(lbRecipientPhone))
                .addGap(101, 101, 101))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(lbRecipientName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(lbRecipientAddress))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(lbRecipientPhone))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(277, 277, 277)
                .addComponent(jLabel1)
                .addContainerGap(283, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(lbOrderID)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 234, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbOrderDate)
                .addGap(125, 125, 125))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrOrderDetails, javax.swing.GroupLayout.DEFAULT_SIZE, 629, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(288, Short.MAX_VALUE)
                .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(278, 278, 278))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lbOrderDate)
                    .addComponent(jLabel2)
                    .addComponent(lbOrderID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addComponent(scrOrderDetails, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnExit)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        // TODO add your handling code here:
        dispose();
        admin.setEnabled(true);
        admin.setAlwaysOnTop(true);
}//GEN-LAST:event_btnExitActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        admin.setEnabled(true);
        admin.setAlwaysOnTop(true);
    }//GEN-LAST:event_formWindowClosed

    /**
    * @param args the command line arguments
    */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lbCustomerAddress;
    private javax.swing.JLabel lbCustomerEmail;
    private javax.swing.JLabel lbCustomerName;
    private javax.swing.JLabel lbCustomerPhone;
    private javax.swing.JLabel lbOrderDate;
    private javax.swing.JLabel lbOrderID;
    private javax.swing.JLabel lbRecipientAddress;
    private javax.swing.JLabel lbRecipientName;
    private javax.swing.JLabel lbRecipientPhone;
    private javax.swing.JScrollPane scrOrderDetails;
    // End of variables declaration//GEN-END:variables

}
